package com.itheima.controller.backend.check;


import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Pageinfo;
import com.itheima.service.backend.CheckService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RequestMapping("/checkitem")
@RestController
public class CheckController {

    @Autowired
    private CheckService checkService;

    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        log.info("新增检查项, CheckItem: {}", checkItem);
        try {
            checkService.add(checkItem);
            return new Result(true,"新增检查项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增检查项失败");
        }

    }

    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id){
        log.info("根据ID删除检查项, id:{}",id);
        checkService.delete(id);
        return new Result(true,"删除成功");
    }

    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam Integer checkgroupId){
        log.info("根据检查组id查询检查项id, checkgroupid:{}",checkgroupId);
        List<Integer> checkItemIds = checkService.findCheckItemIdsByCheckGroupId(checkgroupId);
        return new Result(true,"查询成功",checkItemIds);
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        log.info("编辑检查项, CheckItem: {}", checkItem);
        try {
            checkService.edit(checkItem);
            return new Result(true,"编辑检查项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑检查项失败");
        }
    }

    @PostMapping("findPage")
    public PageResult page(@RequestBody Pageinfo pageinfo
                       ){
        Integer page = pageinfo.getCurrentPage();
        Integer pageSize = pageinfo.getPageSize();
        String name = pageinfo.getQueryString();
        log.info("分页查询, 参数: {},{},{},{},{},{}",page,pageSize,name);
        //调用service分页查询
        PageResult pageResult = checkService.page(page,pageSize,name);
        return pageResult;
    }

    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有检查项");
        List<CheckItem> checkItems = checkService.list();
        return new Result(true,"查询成功",checkItems);
    }

    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id){
        log.info("根据id查询检查项, id:{}",id);
        CheckItem checkItem = checkService.findById(id);
        return new Result(true,"查询成功",checkItem);
    }



}
